/**
 * 处理下啦列表框
 */
(function($) {
	$.fn.dropdown_list = function(a, b, c) {
		var $this = $(this);
		switch (typeof (a)) {
		case "object":
			var default_settings = {
				list : [],
				key : "id",
				value : "text",
				expand : "expand",
				param : {},
				change : function() {
				}
			};
			$.fn.dropdown_list[$this.attr("id")] = {};
			$.fn.dropdown_list[$this.attr("id")]["config"] = $.extend(
					default_settings, a);
			init_dropdown_list($this, a);
			if ($.fn.dropdown_list[$this.attr("id")]["config"].list.length > 0) {
				add_dropdown_list($this,
						$.fn.dropdown_list[$this.attr("id")]["config"].list);
			}
			break;
		case "string":
			switch (a) {
			case "set_list":
				if (b.length > 0) {
					add_dropdown_list($this, b);
				}
				break;

			default:
				break;
			}
			break;

		default:
			break;
		}
	};
	function init_dropdown_list(tag) {
		$.fn.dropdown_list[tag.attr("id")]["menu"] = tag
				.next("ul.dropdown-menu");
		$.fn.dropdown_list[tag.attr("id")]["value"] = tag.children(":first");
		$.fn.dropdown_list[tag.attr("id")]["menu"].on("click", "a", function() {
			$.fn.dropdown_list[tag.attr("id")]["value"].data("value", $(this)
					.data("value"));
			$.fn.dropdown_list[tag.attr("id")]["value"].html($(this).html());
			$.fn.dropdown_list[tag.attr("id")]["config"].change(this,
					$.fn.dropdown_list[tag.attr("id")]["config"].param);
		});
		$.fn.dropdown_list[tag.attr("id")]["menu"].find("a:first").trigger(
				"click");
	}
	function add_dropdown_list(tag, list) {
		var op = new Array();
		var key = $.fn.dropdown_list[tag.attr("id")]["config"].key;
		var value = $.fn.dropdown_list[tag.attr("id")]["config"].value;
		var expand = $.fn.dropdown_list[tag.attr("id")]["config"].expand;
		$.each(list, function(i, n) {
			op.push("<li><a href='#' data-value='");
			op.push(n[key]);
			op.push("' data-expand='");
			op.push(n[expand]);
			op.push("'>");
			op.push(n[value]);
			op.push("</a></li>");
		});
		$.fn.dropdown_list[tag.attr("id")]["menu"].html(op.join(""));
		$.fn.dropdown_list[tag.attr("id")]["menu"].find("a:first").trigger(
				"click");
	}
})(jQuery);